package io.gitee.zhangbinhub.acp.cloud.integration

import io.gitee.zhangbinhub.acp.cloud.*
import io.gitee.zhangbinhub.acp.cloud.conf.AcpCloudLogServerConfiguration
import io.gitee.zhangbinhub.acp.cloud.log.consumer.AcpCloudLogConsumer
import io.gitee.zhangbinhub.acp.cloud.log.consumer.DefaultLogProcess
import io.gitee.zhangbinhub.acp.cloud.log.consumer.LogProcess
import io.gitee.zhangbinhub.acp.core.common.log.LogFactory
import org.noear.solon.Solon
import org.noear.solon.Utils
import org.noear.solon.cloud.CloudClient
import org.noear.solon.cloud.CloudManager
import org.noear.solon.cloud.annotation.CloudEvent
import org.noear.solon.cloud.annotation.EventLevel
import org.noear.solon.core.AppContext
import org.noear.solon.core.Plugin
import org.noear.solon.core.event.AppPluginLoadEndEvent

class XPluginImp : Plugin {
    private val logFactory = LogFactory.getInstance(this.javaClass)
    override fun start(context: AppContext) {
        context.beanMake(AcpCloudComponentAutoConfiguration::class.java)
        context.beanMake(AcpCloudDistributedLockAutoConfiguration::class.java)
        context.beanMake(AcpCloudLoadBalancerAutoConfiguration::class.java)
        context.beanMake(AcpCloudLogAutoConfiguration::class.java)
        context.beanMake(AcpCloudNamiAutoConfiguration::class.java)
        context.app().onEvent(AppPluginLoadEndEvent::class.java) {
            initLogServerConsumer(context)
        }
    }

    private fun initLogServerConsumer(context: AppContext) {
        context.getBean(AcpCloudLogServerConfiguration::class.java).let { acpCloudLogServerConfiguration ->
            if (acpCloudLogServerConfiguration.enabled) {
                logFactory.info("Log Server is enabled!")
                val anno = CloudEvent(
                    value = acpCloudLogServerConfiguration.destination,
                    topic = acpCloudLogServerConfiguration.destination,
                    group = "",
                    tag = "",
                    level = EventLevel.cluster,
                    channel = "",
                    qos = 1
                )
                val handle = AcpCloudLogConsumer(context.getBean(LogProcess::class.java) ?: DefaultLogProcess())
                CloudManager.register(anno, handle)
                if (CloudClient.event() != null) {
                    val topic = Solon.cfg().getByTmpl(Utils.annoAlias(anno.value, anno.topic))
                    val group = Solon.cfg().getByTmpl(anno.group)
                    CloudClient.event().attention(
                        anno.level,
                        anno.channel,
                        group,
                        topic,
                        anno.tag,
                        anno.qos,
                        handle
                    )
                }
                logFactory.info("Register CloudEventHandler<AcpCloudLogConsumer> topic[${acpCloudLogServerConfiguration.destination}]")
            }
        }
    }
}